﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Falcon.Core.SqlServer.Repositories.Readers
{
	using System.Data.SqlClient;
	using Falcon.Core.Domain;
	using Falcon.Core.Domain.Enums;

	public class UserSingleReader : IReader<User>
	{
		/// <summary>
		/// Populates the specified reader.
		/// </summary>
		/// <param name="reader">The reader.</param>
		/// <param name="entity">The entity.</param>
		public void Populate(SqlDataReader reader, User entity)
		{
			// user
			entity.Id = reader.GetInt32(0);
			entity.Name = reader.GetString(1);
			entity.Slug = reader.GetString(2);
			entity.EmailAddress = reader.GetString(3);
			entity.HostAddress = reader.GetString(4); 
			entity.Password = reader.GetString(5);
			entity.Salt = reader.GetString(6);
			entity.Posts = reader.GetInt32(7);
			entity.Reputation = reader.GetInt32(8);
			entity.Status = (UserStatus)reader.GetInt32(9);
			entity.Created = reader.GetDateTime(10);
			entity.Updated = reader.GetDateTime(11);
			entity.LastVisit = reader.GetDateTime(12);
			entity.LastLogOn = reader.GetDateTime(13);

			// user preference
			entity.Preference = new UserPreference
			{
				Id = reader.GetInt32(14),
				Messages = reader.GetInt32(15),
				IsMessagingEnabled = reader.GetBoolean(16),
				IsMessagingRetrictedEnabled = reader.GetBoolean(17),
				IsMessagingNotificationEnabled = reader.GetBoolean(18),
				IsProfileMessagingEnabled = reader.GetBoolean(19),
				IsProfileMessagingRetrictedEnabled = reader.GetBoolean(20),
				IsProfileMessagingNotificationEnabled = reader.GetBoolean(21),
				IsFriendshipNotificationEnabled = reader.GetBoolean(22),
				Dst = (UserPreferenceDaylightSavingTime)reader.GetInt32(23),
				StartOfWeek = (UserPreferenceStartOfWeek)reader.GetInt32(24),
				TimeFormat = (UserPreferenceTimeFormat)reader.GetInt32(25),
				Country = new Country
				{
					Id = reader.GetInt32(26),
					Title = reader.GetString(27),
					Slug = reader.GetString(28),
					Code = reader.GetString(29)
				},
				TimeZone = new TimeZone
				{
					Id = reader.GetInt32(30),
					Title = reader.GetString(31),
					Slug = reader.GetString(32),
					Display = reader.GetString(33),
					Offset = reader.GetDouble(34)
				}
			};

			// user profile
			entity.Profile = new UserProfile
         	{
				Id = reader.GetInt32(35),
         		Title = reader.GetString(36),
         		Homepage = (reader.IsDBNull(37)) ? string.Empty : reader.GetString(37),
         		Icq = (reader.IsDBNull(38)) ? string.Empty : reader.GetString(38),
         		Aim = (reader.IsDBNull(39)) ? string.Empty : reader.GetString(39),
         		Msn = (reader.IsDBNull(40)) ? string.Empty : reader.GetString(40),
         		Yahoo = (reader.IsDBNull(41)) ? string.Empty : reader.GetString(41),
         		Jabber = (reader.IsDBNull(42)) ? string.Empty : reader.GetString(42),
         		Skype = (reader.IsDBNull(43)) ? string.Empty : reader.GetString(43),
				Signature = (reader.IsDBNull(44)) ? string.Empty : reader.GetString(44),
         		SignatureParsed = (reader.IsDBNull(45)) ? string.Empty : reader.GetString(45)
         	};

			if (reader.IsDBNull(46))
			{
				return;
			}

			// user avatar
			entity.Profile.Avatar = new UserAvatar
        	{
				Id = reader.GetInt32(46),
        		FileName = reader.GetString(47),
				FileDiskName = reader.GetString(48),
        		FileType = reader.GetString(49),
        		FilePath = reader.GetString(50)
        	};
		}
	}
}